package cn.iocoder.yudao.module.reading.controller.app.favorite;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.reading.controller.app.favorite.vo.FavoriteCreateReqVO;
import cn.iocoder.yudao.module.reading.controller.app.favorite.vo.FavoriteRemoveReqVO;
import cn.iocoder.yudao.module.reading.controller.app.favorite.vo.FavoriteRespVO;
import cn.iocoder.yudao.module.reading.controller.app.favorite.vo.FavoriteItemRespVO;
import cn.iocoder.yudao.module.reading.service.favorite.FavoriteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * 用户 APP - 收藏管理
 *
 * @author 李晨
 */
@Tag(name = "用户 APP - 收藏管理")
@RestController
@RequestMapping("/reading/favorites")
@Validated
@Slf4j
public class FavoriteController {

    @Resource
    private FavoriteService favoriteService;

    @PostMapping("/add")
    @Operation(summary = "添加收藏")
    @PreAuthenticated
    public CommonResult<Long> addFavorite(@Valid @RequestBody FavoriteCreateReqVO createReqVO) {
        Long userId = getLoginUserId();
        Long favoriteId = favoriteService.addFavorite(userId, createReqVO);
        return success(favoriteId);
    }

    @DeleteMapping("/remove")
    @Operation(summary = "取消收藏")
    @PreAuthenticated
    public CommonResult<Boolean> removeFavorite(@Valid @RequestBody FavoriteRemoveReqVO removeReqVO) {
        Long userId = getLoginUserId();
        favoriteService.removeFavorite(userId, removeReqVO.getTargetType(), removeReqVO.getTargetId());
        return success(true);
    }

    @PostMapping("/toggle")
    @Operation(summary = "切换收藏状态")
    @PreAuthenticated
    public CommonResult<Boolean> toggleFavorite(
            @Parameter(description = "收藏类型", required = true, example = "book_set") @RequestParam("targetType") String targetType,
            @Parameter(description = "目标ID", required = true, example = "1") @RequestParam("targetId") Long targetId,
            @Parameter(description = "收藏备注", example = "很喜欢") @RequestParam(value = "remark", required = false) String remark) {
        Long userId = getLoginUserId();
        boolean isFavorited = favoriteService.toggleFavorite(userId, targetType, targetId, remark);
        return success(isFavorited);
    }

    @GetMapping("/check")
    @Operation(summary = "检查收藏状态")
    @PreAuthenticated
    public CommonResult<Boolean> checkFavoriteStatus(
            @Parameter(description = "收藏类型", required = true, example = "book_set") @RequestParam("targetType") String targetType,
            @Parameter(description = "目标ID", required = true, example = "1") @RequestParam("targetId") Long targetId) {
        Long userId = getLoginUserId();
        boolean isFavorited = favoriteService.isFavorited(userId, targetType, targetId);
        return success(isFavorited);
    }

    @PostMapping("/batch-check")
    @Operation(summary = "批量检查收藏状态")
    @PreAuthenticated
    public CommonResult<Map<Long, Boolean>> batchCheckFavoriteStatus(
            @Parameter(description = "收藏类型", required = true, example = "book_set") @RequestParam("targetType") String targetType,
            @Parameter(description = "目标ID列表", required = true) @RequestBody List<Long> targetIds) {
        Long userId = getLoginUserId();
        Map<Long, Boolean> result = favoriteService.batchCheckFavoriteStatus(userId, targetType, targetIds);
        return success(result);
    }

    @GetMapping("/list")
    @Operation(summary = "获取收藏列表")
    @PreAuthenticated
    public CommonResult<List<FavoriteRespVO>> getFavoriteList(
            @Parameter(description = "收藏类型", example = "book_set") @RequestParam(value = "type", required = false) String type,
            @Parameter(description = "限制数量", example = "20") @RequestParam(value = "limit", required = false) Integer limit) {
        Long userId = getLoginUserId();
        List<FavoriteRespVO> result = favoriteService.getUserFavorites(userId, type, limit);
        return success(result);
    }

    @GetMapping("/count")
    @Operation(summary = "获取收藏数量")
    @PreAuthenticated
    public CommonResult<Long> getFavoriteCount(
            @Parameter(description = "收藏类型", example = "book_set") @RequestParam(value = "type", required = false) String type) {
        Long userId = getLoginUserId();
        Long count = favoriteService.countUserFavorites(userId, type);
        return success(count);
    }

    @DeleteMapping("/clear")
    @Operation(summary = "清空收藏")
    @PreAuthenticated
    public CommonResult<Boolean> clearFavorites(
            @Parameter(description = "收藏类型", example = "book_set") @RequestParam(value = "favoriteType", required = false) String favoriteType) {
        Long userId = getLoginUserId();
        favoriteService.clearUserFavorites(userId, favoriteType);
        return success(true);
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除收藏")
    @PreAuthenticated
    public CommonResult<Boolean> batchDeleteFavorites(@Valid @RequestBody List<FavoriteRemoveReqVO> removeList) {
        Long userId = getLoginUserId();
        for (FavoriteRemoveReqVO removeReqVO : removeList) {
            favoriteService.removeFavorite(userId, removeReqVO.getTargetType(), removeReqVO.getTargetId());
        }
        return success(true);
    }

    // ==================== 分页查询收藏列表的具体端点 ====================

    @GetMapping("/books")
    @Operation(summary = "获取收藏绘本列表")
    @PreAuthenticated
    public CommonResult<PageResult<FavoriteItemRespVO>> getFavoriteBooks(
            @Parameter(description = "页码", example = "1") @RequestParam(value = "page", defaultValue = "1") int page,
            @Parameter(description = "每页大小", example = "20") @RequestParam(value = "size", defaultValue = "20") int size) {
        Long userId = getLoginUserId();
        PageParam pageParam = new PageParam();
        pageParam.setPageNo(page);
        pageParam.setPageSize(size);
        PageResult<FavoriteItemRespVO> result = favoriteService.getUserFavoriteItemsPage(userId, "book", pageParam);
        return success(result);
    }

    @GetMapping("/book-sets")
    @Operation(summary = "获取收藏绘本集列表")
    @PreAuthenticated
    public CommonResult<PageResult<FavoriteItemRespVO>> getFavoriteBookSets(
            @Parameter(description = "页码", example = "1") @RequestParam(value = "page", defaultValue = "1") int page,
            @Parameter(description = "每页大小", example = "20") @RequestParam(value = "size", defaultValue = "20") int size) {
        Long userId = getLoginUserId();
        PageParam pageParam = new PageParam();
        pageParam.setPageNo(page);
        pageParam.setPageSize(size);
        PageResult<FavoriteItemRespVO> result = favoriteService.getUserFavoriteItemsPage(userId, "book_set", pageParam);
        return success(result);
    }

    @GetMapping("/audios")
    @Operation(summary = "获取收藏音频列表")
    @PreAuthenticated
    public CommonResult<PageResult<FavoriteItemRespVO>> getFavoriteAudios(
            @Parameter(description = "页码", example = "1") @RequestParam(value = "page", defaultValue = "1") int page,
            @Parameter(description = "每页大小", example = "20") @RequestParam(value = "size", defaultValue = "20") int size) {
        Long userId = getLoginUserId();
        PageParam pageParam = new PageParam();
        pageParam.setPageNo(page);
        pageParam.setPageSize(size);
        PageResult<FavoriteItemRespVO> result = favoriteService.getUserFavoriteItemsPage(userId, "audio", pageParam);
        return success(result);
    }

}
